<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
    >
    </TopFilter>

    <FilterTable
      ref="table"
      :columns="partColumns"
      :fetchapi="partList"
      :params="fetchParams"
      height="auto"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.partColumns = columns"
      :selectionType="'single'"
    >
    </FilterTable>

    <div :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right' }"
    >
        <el-button @click="closeDrawer">关 闭</el-button>
        <el-button @click="saveDialog" type="primary" :loading="saveLoading" :disabled='isOpen'>确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getpartinfo, addPartRelation } from '@/api/partsmanage/partinfos/partInfoManage/index';
export default {
    name: 'addSubPart',
    props: {
        partData: { type: Object, default: () => ({}) },
        replaceList: { type: Object, default: () => ({}) }
    },
    data() {
        return {
            fetchParams: {
                bjpjfl:this.partData.bjpjfl,
                bjdebz:this.partData.bjdebz,
                bjtsbz:this.partData.bjtsbz,
                exceptId:this.partData.id
            },
            partList: getpartinfo,
            partColumns: this.subPartTable(),
            saveLoading: false,
            topFilterList: this.subPartSearch(),
            selectList: [],
            isOpen: true,
            mainPartId: 0
        }
    },
    created() {
        this.mainPartId = this.partData.id;
    },
    methods: {
        subPartSearch() {
            return [
                {
                    type: 'INPUT',
                    label: '备件代码',
                    fieldName: 'bjxmbh',
                    placeholder: '请输入'
                },
                {
                    type: 'INPUT',
                    label: '备件名称',
                    fieldName: 'bjzwmc',
                    placeholder: '请输入'
                }
            ]
        },
        subPartTable() {
            return [
                {
                    title: '序号',
                    dataIndex: 'index',
                    width: 80,
                    sorter: true,
                    render: props => {
                        return <span>{props.row.index + 1}</span>;
                    }
                },
                {
                    title: '备件代码',
                    dataIndex: 'bjxmbh',
                    sorter: true,
                    filter: true,
                    filterType: 'input',
                },
                {
                    title: '备件名称',
                    dataIndex: 'bjzwmc',
                    sorter: true,
                    filter: true,
                    filterType: 'input',
                },
                {
                    title: '备件价格',
                    dataIndex: 'xs',
                    sorter: true,
                    numberFormat: true
                }
            ]
        },
        changeHandle(val) {
            this.fetchParams={
                bjpjfl:this.partData.bjpjfl,
                bjdebz:this.partData.bjdebz,
                bjtsbz:this.partData.bjtsbz,
                exceptId:this.partData.id
            }
            this.isOpen = true;
            this.fetchParams = Object.assign({},this.fetchParams,val);
        },
        selectChangeHandle(val) {
            if (val.length > 0) {
                let flag=true;
                if (this.replaceList && this.replaceList.length > 0) {
                    for (let dIdx = 0; dIdx < this.replaceList.length; dIdx++) {
                        debugger
                        let tcode = this.replaceList[dIdx].replacePartId;
                            if (tcode === val[0].id) {
                                flag=false;
                                break;
                            }
                    }
                }
                if(flag){
                    this.selectList = val;
                    this.isOpen = false;
                }else{
                    this.isOpen = true;
                    this.selectList = [];
                    this.$notify.error({ title: '提示信息', message: '该替换件已添加，请确认!' });
                }
                
            } else {
                this.isOpen = true;
                this.selectList = [];
            }
        },
   
        //保存
        saveDialog() {
            this.$emit('close', { visible: false, data: this.selectList });
        },

        closeDrawer() {
            this.$emit('close');
        },
        async addRelation() {
            this.saveLoading = true;
            let json = {};
            this.$set(json, 'appId', this.selectList[0].appId);
            this.$set(json, 'mainPartId', this.mainPartId);
            this.$set(json, 'replacePartId', this.selectList[0].id);
            this.$set(json, 'replacePartCode', this.selectList[0].bjxmbh);
            this.$set(json, 'replacePartName', this.selectList[0].bjzwmc);
            this.$set(json, 'replacePartPrice', this.selectList[0].ls);
            console.info(json);
            const res = await addPartRelation(json);
            if (res.resultCode === 200) {
                this.$emit('close', json);
            }
            this.saveLoading = false;
        }
    }
}
</script>